package cn.itcast.service.impl;

import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.mapper.CheckGroupMapper;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.service.CheckGroupService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    CheckGroupMapper mapper;

    @Transactional
    public void add(CheckGroup group, Integer[] checkitemIds) {


        //新增检查组
        mapper.add(group);

        if(checkitemIds!=null && checkitemIds.length>0){

            for(Integer itemId : checkitemIds) {
                //向中间表添加数据
                mapper.setCheckGroupAndCheckItem(group.getId(),itemId);

            }

        }
    }



    public PageResult findPage(QueryPageBean bean) {

        //pageHelper.starpage(当前页码，每页展示条数)
        PageHelper.startPage(bean.getCurrentPage(),bean.getPageSize());

        //将查寻出的结果封装为集合对象
        List<CheckGroup> list = mapper.findPage(bean.getQueryString());

        //将集合载入到pageInfo中，反推出总记录数
        PageInfo pi = new PageInfo(list);

        //将总页数与当前页集合载入到PageResult进行返回
        return  new PageResult(pi.getTotal(),list);
    }



    public void edit(String id) {

    }

    //dubbo
    public CheckGroup findById(String id) {

        CheckGroup cg = mapper.findById(id);

        return cg;
    }



    public List<CheckGroup> findAll() {

        return mapper.findAll();
    }
}
